/*
 * Project: nRF905 AVR/Arduino Library/Driver
 * Author: Zak Kemble, contact@zakkemble.co.uk
 * Copyright: (C) 2017 by Zak Kemble
 * License: GNU GPL v3 (see License.txt)
 * Web: http://blog.zakkemble.co.uk/nrf905-avrarduino-librarydriver/
 */

#ifndef NRF905_DEFS_H_
#define NRF905_DEFS_H_

// Instructions
#define NRF905_CMD_NOP			0xFF
#define NRF905_CMD_W_CONFIG		0x00
#define NRF905_CMD_R_CONFIG		0x10
#define NRF905_CMD_W_TX_PAYLOAD	0x20
#define NRF905_CMD_R_TX_PAYLOAD	0x21
#define NRF905_CMD_W_TX_ADDRESS	0x22
#define NRF905_CMD_R_TX_ADDRESS	0x23
#define NRF905_CMD_R_RX_PAYLOAD	0x24
#define NRF905_CMD_CHAN_CONFIG	0x80

// Registers
#define NRF905_REG_CHANNEL			0x00
#define NRF905_REG_CONFIG1			0x01
#define NRF905_REG_ADDR_WIDTH		0x02
#define NRF905_REG_RX_PAYLOAD_SIZE	0x03
#define NRF905_REG_TX_PAYLOAD_SIZE	0x04
#define NRF905_REG_RX_ADDRESS		0x05
#define NRF905_REG_CONFIG2			0x09


// TODO remove
#define NRF905_REG_AUTO_RETRAN	NRF905_REG_CONFIG1
#define NRF905_REG_LOW_RX		NRF905_REG_CONFIG1
#define NRF905_REG_PWR			NRF905_REG_CONFIG1
#define NRF905_REG_BAND			NRF905_REG_CONFIG1
#define NRF905_REG_CRC			NRF905_REG_CONFIG2
#define NRF905_REG_CLK			NRF905_REG_CONFIG2
#define NRF905_REG_OUTCLK		NRF905_REG_CONFIG2
#define NRF905_REG_OUTCLK_FREQ	NRF905_REG_CONFIG2


// Clock options
#define NRF905_CLK_4MHZ			0x00
#define NRF905_CLK_8MHZ			0x08
#define NRF905_CLK_12MHZ		0x10
#define NRF905_CLK_16MHZ		0x18
#define NRF905_CLK_20MHZ		0x20

// Register masks
#define NRF905_MASK_CHANNEL		0xFE
#define NRF905_MASK_AUTO_RETRAN	~(NRF905_AUTO_RETRAN_ENABLE | NRF905_AUTO_RETRAN_DISABLE) //0xDF
#define NRF905_MASK_LOW_RX		~(NRF905_LOW_RX_ENABLE | NRF905_LOW_RX_DISABLE) //0xEF
#define NRF905_MASK_PWR			~(NRF905_PWR_n10 | NRF905_PWR_n2 | NRF905_PWR_6 | NRF905_PWR_10) //0xF3
#define NRF905_MASK_BAND		~(NRF905_BAND_433 | NRF905_BAND_868 | NRF905_BAND_915) //0xFD
#define NRF905_MASK_CRC			(uint8_t)(~(NRF905_CRC_DISABLE | NRF905_CRC_8 | NRF905_CRC_16)) //0x3F // typecast to stop compiler moaning about large integer truncation
#define NRF905_MASK_CLK			~(NRF905_CLK_4MHZ | NRF905_CLK_8MHZ | NRF905_CLK_12MHZ | NRF905_CLK_16MHZ | NRF905_CLK_20MHZ) //0xC7
#define NRF905_MASK_OUTCLK		~(NRF905_OUTCLK_DISABLE | NRF905_OUTCLK_4MHZ | NRF905_OUTCLK_2MHZ | NRF905_OUTCLK_1MHZ | NRF905_OUTCLK_500KHZ) // 0xF8

// Bit positions
#define NRF905_STATUS_DR		5
#define NRF905_STATUS_AM		7

#include "nRF905_config.h"

#if (NRF905_DR_SW && NRF905_INTERRUPTS)
	#error "NRF905_INTERRUPTS and NRF905_DR_SW cannot both be enabled"
#elif (NRF905_AM_SW && NRF905_INTERRUPTS_AM)
	#error "NRF905_INTERRUPTS_AM and NRF905_AM_SW cannot both be enabled"
#elif (!NRF905_INTERRUPTS && NRF905_INTERRUPTS_AM)
	#error "NRF905_INTERRUPTS_AM cannot be enabled without NRF905_INTERRUPTS"
#endif

#ifndef ARDUINO

	#define CONCAT(a, b) a ## b
	#define CONCAT2(a, b, c) a ## b ## c

	#define PORT(port)			CONCAT(PORT, port)
	#define PORTBIT(port, bit)	CONCAT2(PORT, port, bit)
	#define DDR(port)			CONCAT(DDR, port)
	#define PINPORT(port)		CONCAT(PIN, port)
	#define PINBIT(port, bit)	CONCAT2(PIN, port, bit)
	#define PCINT(pcint)		CONCAT(PCINT, pcint)

	#define TRX_EN_DDR		DDR(NRF905_TRX_EN_PORT)
	#define TRX_EN_PORT		PORT(NRF905_TRX_EN_PORT)
	#define TRX_EN_BIT		PORTBIT(NRF905_TRX_EN_PORT, NRF905_TRX_EN_BIT)

	#define PWR_MODE_DDR	DDR(NRF905_PWR_MODE_PORT)
	#define PWR_MODE_PORT	PORT(NRF905_PWR_MODE_PORT)
	#define PWR_MODE_BIT	PORTBIT(NRF905_PWR_MODE_PORT, NRF905_PWR_MODE_BIT)

	#define TX_EN_DDR		DDR(NRF905_TX_EN_PORT)
	#define TX_EN_PORT		PORT(NRF905_TX_EN_PORT)
	#define TX_EN_BIT		PORTBIT(NRF905_TX_EN_PORT, NRF905_TX_EN_BIT)

	#define CD_DDR			DDR(NRF905_CD_PORT)
	#define CD_PORT			PINPORT(NRF905_CD_PORT)
	#define CD_BIT			PINBIT(NRF905_CD_PORT, NRF905_CD_BIT)

	#define AM_DDR			DDR(NRF905_AM_PORT)
	#define AM_PORT			PINPORT(NRF905_AM_PORT)
	#define AM_BIT			PINBIT(NRF905_AM_PORT, NRF905_AM_BIT)

	#define DR_DDR			DDR(NRF905_DR_PORT)
	#define DR_PORT			PINPORT(NRF905_DR_PORT)
	#define DR_BIT			PINBIT(NRF905_DR_PORT, NRF905_DR_BIT)

	#define CSN_DDR			DDR(NRF905_CSN_PORT)
	#define CSN_PORT		PORT(NRF905_CSN_PORT)
	#define CSN_BIT			PORTBIT(NRF905_CSN_PORT, NRF905_CSN_BIT)

	

	#define INTCONCAT(num)		CONCAT(INT, num)
	#define ISCCONCAT(num, bit)	CONCAT2(ISC, num, bit)
	#define INTVECTCONCAT(num)	CONCAT2(INT, num, _vect)
/*
	#ifndef NRF905_REG_EXTERNAL_INT_DR
		#ifdef EIMSK
			#define NRF905_REG_EXTERNAL_INT_DR EIMSK
		#elif defined GICR
			#define NRF905_REG_EXTERNAL_INT_DR GICR
		#else
			#define NRF905_REG_EXTERNAL_INT_DR GIMSK
		#endif
	#endif
*/
	#ifndef NRF905_REG_EXTERNAL_INT_CTL_DR
		#ifdef EICRA
			#if NRF905_INTERRUPT_NUM_DR < 4
				#define NRF905_REG_EXTERNAL_INT_CTL_DR EICRA
			#else
				#define NRF905_REG_EXTERNAL_INT_CTL_DR EICRB
			#endif
		#else
			#define NRF905_REG_EXTERNAL_INT_CTL_DR MCUCR
		#endif
	#endif
/*
	#ifndef NRF905_BIT_EXTERNAL_INT_DR
		#define NRF905_BIT_EXTERNAL_INT_DR INTCONCAT(NRF905_INTERRUPT_NUM_DR)
	#endif
*/
	#ifndef NRF905_BIT_EXTERNAL_INT_CTL_DR
		#define NRF905_BIT_EXTERNAL_INT_CTL_DR (_BV(ISCCONCAT(NRF905_INTERRUPT_NUM_DR, 1))|_BV(ISCCONCAT(NRF905_INTERRUPT_NUM_DR, 0))) // Rising
	#endif

	#ifndef NRF905_INT_VECTOR_DR
		#define NRF905_INT_VECTOR_DR INTVECTCONCAT(NRF905_INTERRUPT_NUM_DR)
	#endif


/*
	#ifndef NRF905_REG_EXTERNAL_INT_AM
		#ifdef EIMSK
			#define NRF905_REG_EXTERNAL_INT_AM EIMSK
		#elif defined GICR
			#define NRF905_REG_EXTERNAL_INT_AM GICR
		#else
			#define NRF905_REG_EXTERNAL_INT_AM GIMSK
		#endif
	#endif
*/
	#ifndef NRF905_REG_EXTERNAL_INT_CTL_AM
		#ifdef EICRA
			#if NRF905_INTERRUPT_NUM_AM < 4
				#define NRF905_REG_EXTERNAL_INT_CTL_AM EICRA
			#else
				#define NRF905_REG_EXTERNAL_INT_CTL_AM EICRB
			#endif
		#else
			#define NRF905_REG_EXTERNAL_INT_CTL_AM MCUCR
		#endif
	#endif
/*
	#ifndef NRF905_BIT_EXTERNAL_INT_AM
		#define NRF905_BIT_EXTERNAL_INT_AM INTCONCAT(NRF905_INTERRUPT_NUM_AM)
	#endif
*/
	#ifndef NRF905_BIT_EXTERNAL_INT_CTL_AM
		#define NRF905_BIT_EXTERNAL_INT_CTL_AM (_BV(ISCCONCAT(NRF905_INTERRUPT_NUM_AM, 0))) // Any change
	#endif

	#ifndef NRF905_INT_VECTOR_AM
		#define NRF905_INT_VECTOR_AM INTVECTCONCAT(NRF905_INTERRUPT_NUM_AM)
	#endif

#endif

#endif /* NRF905_DEFS_H_ */
